<?php
class BaseInvitesController extends AppController
{
	var $name= 'Invites';
	var $helpers = array('Html','Form');
	var $components = array('Email','Cms.GigaMail');
	var $uses = array();
	
	function inviteFriends()
	{
		if(!empty($this->data))
		{			
			foreach($this->data['Invite'] as $emailData)
			{	
				if($emailData['email']!='')
				{						
					$this->set('friend_name',$emailData['name']);
					$this->set('name',$emailData['name']);
					//prd($emailData);
					$this->Email->to = $emailData['email'];
					$this->__setEmailParams();					
					$this->Email->send();
					$this->Email->reset();
				}
			}
			$this->successMsg(__('Thank you for inviting your friend(s).'));
			$this->redirect('/');
		}
		else
		{	
			$filename = APP.'plugins'.DS.'invites'.DS.'views'.DS.'elements'.DS.'email'.DS.'html'.DS.'friend_invitation_default.ctp';
			$handle =fopen($filename,'r');
			$contents = @fread($handle, filesize($filename));
			$this->data['Invite']['body'] = $contents;
			fclose($handle);
			
		}
		
	}

	function __setEmailParams()
	{
		if(!isset($this->data['InviteTemp']['title']))
		{
			$this->data['InviteTemp']['title'] ='';
			$this->data['InviteTemp']['body'] ='';
		}
		$this->Email->setFrom($this->data['Sender']['email'],$this->data['Sender']['name']);
		$this->Email->subject = $this->data['InviteTemp']['title'];
		$this->Email->template = 'friend_invitation';
		$this->Email->sendAs = 'both';
		$this->set('body',$this->data['InviteTemp']['body']);
	}
	
	function formatted_inviteFriends($content_id = null)
	{		
		//for wallpaper project.
		$contentDetail = ClassRegistry::init('Content')->find('first',array('conditions'=>array('Content.id'=>$content_id),'recursive'=>-1));
		$contentType = $contentDetail['Content']['type'];
		if(empty($content_id))
		{
			$this->redirect('/');
		}
		$view_url =Configure::read('cmsConfig.Backend.siteUrl').$contentDetail['Content']['url'];
		$view_url = '<a href="'.$view_url.'">'.$view_url.'</a>';
		if(!empty($this->data))
		{			
			foreach($this->data['Invite'] as $emailData)
			{	
				$subject = $this->data['Yourdata']['your_name'].' wants you to check out a '.$contentType;
				if($emailData['email']!='')
				{				
					$this->set('name',$emailData['name']);
					$this->set('your_name',$this->data['Yourdata']['your_name']);
					$this->set('view_url',$view_url);

					$this->Email->to = $emailData['email'];
					$this->Email->subject = $subject;
					$this->Email->template = 'friend_invitation';
					$this->Email->setFrom($this->data['Yourdata']['email'],$this->data['Yourdata']['your_name']);
					$this->Email->sendAs = 'both';
					$this->set('body',nl2br($this->data['InviteTemp']['body']));
					$this->Email->send();
					$this->Email->reset();
				}
			}
			$this->successMsg(sprintf(__('Thank you for sharing this %s with your friend(s).'),low($contentType)));
			$this->redirect('/');
		}
		else
		{	// body code for wallpaper project invitation
			$this->data['Invite']['body'] = 'Dear [[name]], \n [[your_name]] would like to share this '.$contentType.' '.$view_url.' with you';
		}
		$this->set('content_id',$content_id);
	}

}
?>